﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using POI.eTransmittal.BusinessRule;
using POI.eTransmittal.Enitity;

namespace POI.eTransmittal.WebApp
{
    public partial class Contacts : BasePage 
    {
        const string SESSION_CONTACT_ID = "poi.etransmittal_contacts_contactid";
        protected void Page_Init(object sender, EventArgs e)
        {
            RegisterXpodatasource(xpodsContactInfo, xpodsContacts);   
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void gridContactInfo_BeforePerformDataSelect(object sender, EventArgs e)
        {
            Session[SESSION_CONTACT_ID] = (sender as ASPxGridView).GetMasterRowKeyValue(); 
        }

        protected void gridContacts_RowInserted(object sender, DevExpress.Web.Data.ASPxDataInsertedEventArgs e)
        {
            CurrentUnitOfWork.CommitChanges();
        }

        protected void gridContacts_RowUpdated(object sender, DevExpress.Web.Data.ASPxDataUpdatedEventArgs e)
        {
            CurrentUnitOfWork.CommitChanges();
        }

        protected void gridContacts_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
        {
            string id = e.Values["ID"].ToString();
            CommonBusinessRules.DeleteContact(CurrentUnitOfWork, id);

        }

        protected void gridContactInfo_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {

            CommonBusinessRules.CreateContactFaxInfo(CurrentUnitOfWork,Session[SESSION_CONTACT_ID], e.NewValues["Value"]);
          
        }

        protected void gridContactInfo_RowInserted(object sender, DevExpress.Web.Data.ASPxDataInsertedEventArgs e)
        {
           //CurrentUnitOfWork.CommitChanges();
        }

        protected void gridContactInfo_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            if (e.NewValues["Value"] == null)
          throw new Exception("Fax cannot be empty");
        }

        protected void gridContactInfo_RowUpdated(object sender, DevExpress.Web.Data.ASPxDataUpdatedEventArgs e)
        {
            CurrentUnitOfWork.CommitChanges();
        }

        protected void gridContactInfo_RowDeleted(object sender, DevExpress.Web.Data.ASPxDataDeletedEventArgs e)
        {
            CurrentUnitOfWork.CommitChanges();
        }

        protected void gridContactInfo_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
        {

        }

        protected void gridContacts_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            if (e.NewValues["Name"] == null)
                throw new Exception("Name cannot be empty");
        
        }

        protected void gridContacts_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            if (e.NewValues["Name"] == null)
                throw new Exception("Name cannot be empty");
            if (CommonBusinessRules.ContactNameExist(CurrentUnitOfWork, e.NewValues["Name"].ToString()) && e.NewValues["Name"]!=e.OldValues["Name"])
                throw new Exception("Contact already exist");
        }
    }
}